<?php

/**
 * alipay用户项目服务
 */

namespace App\Services\Alipay;


use App\Repositories\Alipay\AlipayUserRepository;
use App\Repositories\Alipay\AlipayUserXCXRepository;



class AlipayUserService
{
    private $alipayUserRepository;
    private $alipayUserXCXRepository;
    private $config = [];

    public function __construct(
        AlipayUserRepository $alipayUserRepository,
        AlipayUserXCXRepository $alipayUserXCXRepository
    )
    {
        $this->alipayUserRepository = $alipayUserRepository;
        $this->alipayUserXCXRepository = $alipayUserXCXRepository;
    }

    public function setConfig($config)
    {
        $this->config = $config;
        $this->alipayUserRepository->_config($config);
        $this->alipayUserXCXRepository->_config($config);
    }

    private function factory()
    {
        switch ($this->config['bindRef']) {
            case 40:
                $objRepository = $this->alipayUserRepository;
                break;
            case 50:
                $objRepository = $this->alipayUserXCXRepository;
                break;
            default :
                $objRepository = $this->alipayUserRepository;
                break;
        }

        return $objRepository;
    }

    public function authorizeUrl($scopes, $callback)
    {
        $getTargetUrl = $this->alipayUserRepository->authorizeUrl($scopes, $callback);

        return $getTargetUrl;
    }


    public function authorizeToken($code, $extend)
    {
        $obj = $this->factory();

        return $obj->authorizeToken($code, $extend);
    }


    public function authorizeUser($openID, $accessToken)
    {
        return $this->alipayUserRepository->authorizeUser($openID, $accessToken);
    }
}